#!/bin/bash
set -euo pipefail
BaseDir=$(cd "$(dirname "$0")"; pwd)
cd ${BaseDir}

op=${1-""}

function consoleStart(){
    echo "加入neubula/run.s运行起来的nebula网络..."
    docker compose up nebula-console -d
    sleep 2
}
function consoleDown(){
    docker compose down
}
#
function consoleImp(){
    local file=$1
    echo "导入文件 ${file}"

    #docker run --rm -ti --network nebulacore_nebula-net -v$(pwd)${localPath}:ngsql --entrypoint=sh docker.m.daocloud.io/vesoft/nebula-console:v3.8.0 -c "echo import; nebula-console -u root -p root --address=graphd --port=9669 --file=/opt/ngsql/${file}"
    docker compose exec -i nebula-console sh -c "echo import; nebula-console -u root -p root --address=graphd --port=9669 --file=/opt/ngsql/${file}"
}
function consoleShell(){
    local exp=$1
    echo "run: $exp ..."
    docker compose exec -i nebula-console sh -c "nebula-console -u root -p root --address=graphd --port=9669 -e '${exp}'"
}

##
case "${op}" in
openstack)
    #openstack的案例
    pushd ./openstatckOps/sample_data
        if [ -f ./edges.ngql ]; then
            echo "ngql files exist"
        else
            #使用schema.ngql的 openstack 空间
            echo 'USE openstack;' > edges.ngql
            cat edges/*.ngql >> edges.ngql
            echo 'USE openstack;' > vertices.ngql
            cat vertices/*.ngql >> vertices.ngql
        fi
    popd

    echo "导入openstack的案例数据"
    consoleStart
    consoleImp openstatckOps/sample_data/schema.ngql
    consoleImp openstatckOps/sample_data/edges.ngql
    consoleImp openstatckOps/sample_data/vertices.ngql
    consoleDown
;;
search-openstack)
    consoleStart
    #openstack的查询语句例子
    consoleImp queryOpenstack.ngql
    consoleDown
;;

##
k8s)
# k8s例子的数据
    echo 'k8s'
    consoleStart
    consoleShell 'CREATE SPACE `k8s` (partition_num = 10, replica_factor = 1, vid_type = FIXED_STRING(256));'
    consoleImp k8sOps/data/graph.ngql
    consoleDown
;;


#
jupyter)
 # https://siwei.io/nebulagraph-in-jupyter-notebook/ 这个很棒
    echo "pyhton交互窗口执行类似如下的例子"
    cat <<EOF
python镜像已经安装好 ipython-ngql
使用openstack的数据查询测试
%load_ext ngql
%ngql --address graphd --port 9669 --user root --password root
%ngql USE openstack
%ngql match h=(host:nova_host) return h

绘图
%ng_draw

%ngql help 可以获得更多帮助信息。
EOF
    pushd ./jupyter
        docker compose up -d
        echo " http://localhost:8888/ root 即可进入jupyter lab"
    popd
;;
jupyter-down)
    pushd ./jupyter
        docker compose down
    popd
;;

#
cstart)
   consoleStart
;;
cdown)
   consoleDown
;;
csh)
    docker compose exec -it nebula-console sh -c "nebula-console -u root -p root --address=graphd --port=9669"
;;

##
*)
    echo " ./runcases.sh openstack k8s| search-openstack"
    echo " ./runcases.sh cstart cdown csh"
    echo " ./runcases.sh jupyter jupyter-down"
    echo " nebula的基础服务，进入nebula/run.sh查看和执行"
;;
esac

echo "done"
